const mysql = require('mysql2');
const winston = require('./logger');

const pool = mysql.createPool({
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});

// 测试连接
pool.getConnection((err, connection) => {
    if (err) {
        winston.error('数据库连接失败:', err);
        return;
    }
    winston.info('新的数据库连接已建立', { threadId: connection.threadId });
    connection.release();
});

// 导出 promise 包装的连接池
const promisePool = pool.promise();
module.exports = promisePool; 